/* 
 * File:   KeyTranslator.h
 * Author: frank
 *
 * Created on December 6, 2014, 10:32 AM
 */

#ifndef KEYTRANSLATOR_H
#define	KEYTRANSLATOR_H

#include <stdbool.h>
#include <stdint.h>

typedef void(*KeyAction)(uint16_t);
typedef void(*CommandResponse)(uint8_t command, bool success, uint8_t data);

#define MAX_KEYBOARDS 5
#define BYTES_PER_KEY 6		// When the helper makes the array this is pretty much fixed.  Will need to modify helper!
#define TRANSLATION_SIZE (BYTES_PER_KEY + 2)
#define USB_ID 0xFF
#define KEY_PRESS 0x0000
#define KEY_RELEASE 0x8000

// Initializes the translator.  Must be called first.
void TranslatorInit();
// Allocate space for 1 keyboard.  May have many.  Returns ID of keyboard.
uint8_t AllocateTranslator(uint8_t size);
// Releases all translators
void DeallocateTranslators();
// Release a single translator
void DeallocateTranslator(uint8_t id);
// Sets the callback for a particular translator
void TranslatorCallback(uint8_t id, KeyAction keyAction, CommandResponse commandResponse);

// Must define a key press and release translation.
// If the code is 0x00 then it is unused.
// First 3 are for press and last 3 are for release.
void AddConversion(uint8_t id, uint16_t translation, uint8_t codes[BYTES_PER_KEY]);
// Do not perform any translation
void ResetConversions(uint8_t id);

// Helpers to make key translation table
// Used for PS2 data
void CreateTranlatedData(uint8_t* codes, uint8_t press1, uint8_t press2, uint8_t press3, uint8_t release1, uint8_t release2, uint8_t release3);
// Used for USB data
void CreateTranslatedData(uint8_t* codes, uint8_t press1, uint8_t release1);

// Start a matrix download
uint8_t BeginKeyTranslationMatrix(uint8_t id, uint8_t size);
// Convert incoming bytes into a key translation matrix.
// Data is low byte of the 16 bit code then high byte of the 16 bit code.
// Then comes the 6 bytes from the codes array starting with index 0 and
// ending with index 5.
bool AddKeyTranslationMatrix(uint8_t data);
// Abort the creation and clean up everything done so far.
void AbortCreateKeyTranslationMatrix();

// The raw data from keyboard to translate
void Translate(uint8_t id, uint8_t code1, uint8_t code2, uint8_t code3);

#endif	/* KEYTRANSLATOR_H */

